<?php
// +----------------------------------------------------------------------
// | Author: 林子彦 
// +----------------------------------------------------------------------

namespace Admin\Model;

/**
 * 插件模型
 * @author yangweijie <yangweijiester@gmail.com>
 */
use Common\Model\RpcUseModel;

class MembersModel extends RpcUseModel {

    protected $_validate = array(
        array('nickname', '1,16', '昵称长度为1-16个字符', self::EXISTS_VALIDATE, 'length')
            //array('nickname', '', '昵称被占用', self::EXISTS_VALIDATE, 'unique'), //用户名被占用
    );
    protected $_auto = array(
        array('birthday', 'strtotime', self::MODEL_BOTH, 'function'),
        //array('logo', 'setLogoUrl', self::MODEL_BOTH, 'callback'),
        array('reg_ip', 'get_client_ip', self::MODEL_INSERT, 'function', 1),
        array('reg_time', NOW_TIME, self::MODEL_INSERT),
        array('status', 'getStatus', self::MODEL_BOTH, 'callback'),
        array('update_time', NOW_TIME, 3)
    );

    public function getStatus() {
        return TRUE;
    }

    /**
     * 登录指定用户
     * @param  integer $uid 用户ID
     * @return boolean      ture-登录成功，false-登录失败
     */
    public function login($uid) {

        $clientapi = $this->client->useService("User");
        $user = $clientapi->getUserInfo($uid);
        /* 检测是否在当前应用注册 */
        if (!$user) {
            $this->error = '用户不存在！'; //应用级别禁用

            return false;
        } else if (1 != $user['status']) {
            if ($user['status'] == 0) {
                $this->error = '用户在审核状态！';
                return false;
            } else {
                $this->error = '用户被拒绝！';
                return false;
            }
        }

        /* 登录用户 */
        $this->autoLogin($user);
        return true;
    }

    /**
     * 添加用户
     * @param  integer $uid 用户ID
     * @return boolean      ture-登录成功，false-登录失败
     */
    public function regadd($data) {

        $clientapi = $this->client->useService("User");
        $user = $clientapi->addMemberData($data);
        return $user;
    }

    /**
     * 自动登录用户
     * @param  integer $user 用户信息数组
     */
    private function autoLogin($user) {
   

        /* 更新登录信息 */
        $data = array(
            'uid' => $user['uid'],
            'login' => array('exp', '`login`+1'),
            'last_login_time' => NOW_TIME,
            'last_login_ip' => get_client_ip(1),
            
        );
        $clientapi = $this->client->useService("User");
        $clientapi->saveMemberData($data); //更新数据


        /* 记录登录SESSION和COOKIES */
        $auth = array(
            'uid' => $user['uid'],
            'username' => $user['nickname'],
            'last_login_time' => $user['last_login_time'],
            'logo' => $user['logo'],
        );

        session('lqwsys_auth', $auth);
        session('lqwsys_auth_sign', data_auth_sign($auth));
    }

    public function getError() {
        return $this->error;
    }

    /**
     * 注销当前用户
     * @return void
     */
    public function logout() {
        session('lqwsys_auth', null);
        session('lqwsys_auth_sign', null);
    }

}
